var xmlBuilder = require("xmlbuilder");
var request = require("request");
module.exports = {
	get: function(req, res) {
		console.log("tile update on " + (new Date()).toLocaleString());
		var data = req.query;
		console.log("query:", data);
		var date = new Date();
		var year = date.getFullYear();
		var month = (date.getMonth() + 1 > 9) ? (date.getMonth() + 1) : ("0" + (date.getMonth() + 1));
		var days = (date.getDate() > 9) ? date.getDate() : ("0" + date.getDate());
		var hours = (date.getHours() > 9) ? date.getHours() : ("0" + date.getHours());
		var minutes = (date.getMinutes() > 9) ? date.getMinutes() : ("0" + date.getMinutes());
		var seconds = (date.getSeconds() > 9) ? date.getSeconds() : ("0" + date.getSeconds());
		var _time = year + "" + month + "" + days + "" + hours + "" + minutes + "" + seconds;
		console.log("get start!"); // 从这里开始
		var url = 'http://route.showapi.com/9-2'; // 城市访问天气接口
		var form = {
			showapi_appid: '5653',
			showapi_sign: "f668fa7e626043b19b34a61743fcf271",
			showapi_timestamp: _time,
			needMoreDay: 1,
			needIndex: 1,
			from: 3,
			area: data.area
		};
		//  天气主题背景
		var weatherThemeList = {
			"暴雪": "daxue.png",
			"暴雨": "baoyu.png",
			"暴雨到大暴雨": "baoyu.png",
			"大暴雨": "baoyu.png",
			"大暴雨到特大暴雨": "baoyu.png",
			"大到暴雪": "daxue.png",
			"大到暴雨": "dayu.png",
			"大雪": "daxue.png",
			"大雨": "dayu.png",
			"冻雨": "dongyu.png",
			"多云": "cloud.png",
			"浮尘": "yangsha.png",
			"雷阵雨": "leizhenyu.png",
			"雷阵雨伴有冰雹": "bingbao.png",
			"霾": "mai.png",
			"强沙尘暴": "shachenbao.png",
			"晴": "sun.png",
			"沙尘暴": "shachebao.png",
			"特大暴雨": "baoyu.png",
			"无": "sun.png",
			"雾": "wu.png",
			"小到中雪": "xiaoxue.png",
			"小到中雨": "xiaoyu.png",
			"小雪": "xiaoxue.png",
			"小雨": "xiaoyu.png",
			"扬沙": "yangsha.png",
			"阴": "cloudy.png",
			"雨夹雪": "dongyu.png",
			"阵雪": "daxue.png",
			"阵雨": "dayu.png",
			"中到大雪": "zhongxue.png",
			"中到大雨": "dayu.png",
			"中雪": "zhongxue.png",
			"中雨": "xiaoyu.png"
		};
		//  天气获取入口
		request.post({
				url: url,
				form: form,
				encoding: 'utf8'
			},
			function(error, response, body) {
				if (response.statusCode == 200) {
					console.log("response!");
					var weather = JSON.parse(body).showapi_res_body;
					// console.log(weather);
					if (!weather.now) {
						console.log("error:", JSON.parse(body));
						res.end();
						return;
					};
					var today = {
						city: weather.cityInfo.c3,
						temperature: weather.now.temperature + "ºC ",
						weather: weather.now.weather,
						wind: weather.now.wind_direction + " " + weather.now.wind_power,
						sun_to_end: weather.f1.sun_begin_end
					};
					var day1 = weather.f1;
					var day2 = weather.f2;
					var day3 = weather.f3;
					var day4 = weather.f4;
					var day5 = weather.f5;
					var xml = xmlBuilder.create({
						tile: {
							visual: {
								"@version": 2,
								"#list": [{
									binding: {
										"@template": "TileSquare150x150PeekImageAndText01",
										"@fallback": "TileSquarePeekImageAndText01",
										image: {
											"@id": 1,
											"@src": "/images/weather/square/" + weatherThemeList[today.weather],
											"@alt": today.city + " " + today.weather
										},
										"#list": [{
											text: {
												"@id": 1,
												"#text": today.city
											}
										}, {
											text: {
												"@id": 2,
												"#text": today.temperature + " " + today.weather
											}
										}, {
											text: {
												"@id": 3,
												"#text": today.wind
											}
										}, {
											text: {
												"@id": 4,
												"#text": "日出：" + today.sun_to_end
											}
										}]
									}
								}, {
									binding: {
										"@template": "TileWide310x150PeekImage02",
										"@fallback": "TileWidePeekImage02",
										image: {
											"@id": 1,
											"@src": "/images/weather/wide/" + weatherThemeList[today.weather],
											"@alt": today.city + " " + today.weather
										},
										"#list": [{
											text: {
												"@id": 1,
												"#text": today.city + " 星期" + _numToChinese(day1.weekday) + " " + today.temperature + " " + today.weather + " " + today.wind
											}
										}, {
											text: {
												"@id": 2,
												"#text": "星期" + _numToChinese(day2.weekday) + " " + day2.day_air_temperature + "～" + day2.night_air_temperature + "ºC " + day2.day_weather + " " + day2.sun_begin_end
											}
										}, {
											text: {
												"@id": 3,
												"#text": "星期" + _numToChinese(day3.weekday) + " " + day3.day_air_temperature + "～" + day3.night_air_temperature + "ºC " + day3.day_weather + " " + day3.sun_begin_end
											}
										}, {
											text: {
												"@id": 4,
												"#text": "星期" + _numToChinese(day4.weekday) + " " + day4.day_air_temperature + "～" + day4.night_air_temperature + "ºC " + day4.day_weather + " " + day4.sun_begin_end
											}
										}, {
											text: {
												"@id": 5,
												"#text": "星期" + _numToChinese(day5.weekday) + " " + day5.day_air_temperature + "～" + day5.night_air_temperature + "ºC " + day5.day_weather + " " + day5.sun_begin_end
											}
										}]
									}
								}]
							}
						}
					}).end({
						pretty: true
					});
					console.log("response end!");
					res.send(xml.toString());
					// res.send(JSON.parse(body).showapi_res_body);
				} else {
					console.log(response.statusCode);
					res.end();
				}
			});
	}
};
// public function
function _numToChinese(num) {
	if (num == 1) {
		return '一';
	};
	if (num == 2) {
		return '二';
	};
	if (num == 3) {
		return '三';
	};
	if (num == 4) {
		return '四';
	};
	if (num == 5) {
		return '五';
	};
	if (num == 6) {
		return '六';
	};
	if (num == 7) {
		return '日';
	};
};